package com.kfm.wiki.controller;

import com.github.pagehelper.PageInfo;
import com.kfm.wiki.entity.Content;
import com.kfm.wiki.entity.Doc;
import com.kfm.wiki.service.DocService;
import com.kfm.wiki.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private DocService docService;

    @GetMapping("/all")
    public R all(Doc doc){
        return R.ok(docService.all(doc));
    }

    @GetMapping("/list")
    public R list(@RequestParam(value = "current", defaultValue = "1") Integer current
            ,@RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                  Doc doc){
        PageInfo<Doc> list = docService.list(current, pageSize, doc);
        return R.ok(list);
    }

    @PostMapping
    public R save(@RequestBody @Validated Doc doc, BindingResult result){
        if (result.hasErrors()){
            return R.failed("请按要求传递参数");
        }
        docService.save(doc);
        return R.ok();
    }


    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id){
        docService.deleteCascade(id);
        return R.ok();
    }

    @GetMapping("/vote/{id}")
    public R vote(@PathVariable("id") Long id){
        docService.incrementVote(id);
        return R.ok();
    }

}
